{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python 按键(key)或值(value)对字典进行排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 给定一个字典，然后按键(key)或值(value)对字典进行排序。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 按(key/value)排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-20T18:59:42.368759Z",
     "start_time": "2020-08-20T18:59:42.364722Z"
    }
   },
   "outputs": [],
   "source": [
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-20T19:18:35.232139Z",
     "start_time": "2020-08-20T19:18:35.223742Z"
    }
   },
   "outputs": [],
   "source": [
    "def dictionairy():\n",
    "    value = {}\n",
    "    value = range_dict()\n",
    "    #print_dict(value)\n",
    "    #sort_dict_key(value)\n",
    "    sort_dict_value(value)\n",
    "    \n",
    "def range_dict():\n",
    "    value = {}\n",
    "    for i in range(20):\n",
    "        value[i] = random.randint(0,101)\n",
    "    return value\n",
    "        \n",
    "def print_dict(data):\n",
    "    for i in data:\n",
    "        print((i,data[i]),end = \" \")\n",
    "        \n",
    "def sort_dict_key(data):\n",
    "    for i in sorted(data):\n",
    "        print((i,data[i]),end = \" \")\n",
    "        \n",
    "def sort_dict_value(data):\n",
    "    print(sorted(data.items(), key = lambda kv:kv[1]))\n",
    "    \n",
    "#     默认升序，降序只须增加reverse=True\n",
    "#     print(sorted(data.items(), key = lambda kv:kv[1], reverse=True))\n",
    "#     等价于\n",
    "#     print(sorted(data.items(), key = lambda kv:(kv[1], kv[0])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-20T19:18:36.960793Z",
     "start_time": "2020-08-20T19:18:36.956725Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(0, 3), (3, 3), (10, 16), (17, 22), (9, 28), (16, 30), (6, 32), (12, 35), (4, 36), (18, 40), (19, 40), (8, 41), (14, 51), (13, 53), (5, 61), (15, 62), (11, 68), (2, 69), (7, 73), (1, 94)]\n"
     ]
    }
   ],
   "source": [
    "dictionairy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-20T19:00:19.201783Z",
     "start_time": "2020-08-20T19:00:19.195699Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n"
     ]
    }
   ],
   "source": [
    "# 测试\n",
    "print(random.randint(5,20))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 字典列表排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-20T19:27:50.032292Z",
     "start_time": "2020-08-20T19:27:50.026313Z"
    }
   },
   "outputs": [],
   "source": [
    "list_data = [\n",
    "    {\"name\":\"Aaobao\", \"age\":100},\n",
    "    {\"name\":\"Bunoob\",\"age\":7},\n",
    "    {\"name\":\"Coogle\",\"age\":19},\n",
    "    {\"name\":\"Diki\",\"age\":20}\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-20T19:24:23.023779Z",
     "start_time": "2020-08-20T19:24:23.017466Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表通过 age 升序排序: \n",
      "[{'name': 'Runoob', 'age': 7}, {'name': 'Google', 'age': 19}, {'name': 'Wiki', 'age': 20}, {'name': 'Taobao', 'age': 100}]\n"
     ]
    }
   ],
   "source": [
    "# 通过 age 升序排序\n",
    "print(\"列表通过 age 升序排序: \")\n",
    "print(sorted(list_data, key = lambda i:i[\"age\"]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-20T19:31:26.314737Z",
     "start_time": "2020-08-20T19:31:26.307979Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表通过 age 和 name 排序: \n",
      "[{'name': 'Bunoob', 'age': 7}, {'name': 'Coogle', 'age': 19}, {'name': 'Diki', 'age': 20}, {'name': 'Aaobao', 'age': 100}]\n"
     ]
    }
   ],
   "source": [
    "# 按 age 排序\n",
    "print (\"列表通过 age 和 name 排序: \")\n",
    "print(sorted(list_data,  key = lambda i:(i[\"age\"],i[\"name\"])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-20T19:32:37.823276Z",
     "start_time": "2020-08-20T19:32:37.816388Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表通过 age 和 name 排序: \n",
      "[{'name': 'Diki', 'age': 20}, {'name': 'Coogle', 'age': 19}, {'name': 'Bunoob', 'age': 7}, {'name': 'Aaobao', 'age': 100}]\n"
     ]
    }
   ],
   "source": [
    "# 按 name 排序\n",
    "print (\"列表通过 age 和 name 排序: \")\n",
    "print(sorted(list_data,  key = lambda i:(i[\"name\"],i[\"age\"]),reverse=True))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "165.675px"
   },
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
